import {
  fakeInsertGoodsCategory,
  fakeUpdateGoodsCategory,
  fakeDeleteGoodsCategory,
  fetchListGoodsCategory,
  fetchListOfflineGoods,
  fetchOfflineGoodsDetail,
  fakeDeleteOfflineGoods,
  fakeDownOfflineGoods,
  fakeSaveOfflineGoods,
  fakeUpOfflineGoods,
  fakeCheckRemoveGoods,
} from '@/services/CommodityServices';
import { notification } from 'antd';
export default {
  namespace: 'commodity',
  state: {
    offlineGoodsList: {
      list: [],
      total: 0,
    },
  },
  effects: {
    *fetchListGoodsCategory({ payload, callback }, { call, put }) {
      const response = yield call(fetchListGoodsCategory, payload);
      if (!response) return;
      const { goodsCategoryList } = response.content;
      callback && callback(goodsCategoryList);
    },

    *fakeDeleteGoodsCategory({ payload, callback }, { call, put }) {
      const response = yield call(fakeDeleteGoodsCategory, payload);
      if (!response) return;
      notification.success({
        message: '温馨提示',
        description: `删除成功`,
      });
      callback && callback();
    },

    *fakeHandleCategory({ payload, callback }, { call, put }) {
      const { id } = payload;
      const response = yield call(id ? fakeUpdateGoodsCategory : fakeInsertGoodsCategory, payload);
      if (!response) return;
      notification.success({
        message: '温馨提示',
        description: id ? '修改成功' : `新增成功`,
      });
      callback && callback();
    },
    *fetchListOfflineGoods({ payload, callback }, { call, put }) {
      const response = yield call(fetchListOfflineGoods, payload);
      if (!response) return;
      const {
        content: { goodsOfflineList, total },
      } = response;
      yield put({
        type: 'save',
        payload: {
          offlineGoodsList: {
            list: goodsOfflineList,
            total: total,
          },
        },
      });
      callback && callback();
    },
    *fetchOfflineGoodsDetail({ payload, callback }, { call, put }) {
      const response = yield call(fetchOfflineGoodsDetail, payload);
      if (!response) return;
      const { content = {} } = response;

      callback && callback(content);
    },
    *fakeSaveOfflineGoods({ payload, callback }, { call, put }) {
      const { id } = payload;
      const response = yield call(fakeSaveOfflineGoods, payload);
      if (!response) return;
      notification.success({
        message: '温馨提示',
        description: id ? '修改成功' : '新增成功',
      });
      callback && callback();
    },
    *fakeDownOfflineGoods({ payload, callback }, { call, put }) {
      const response = yield call(fakeDownOfflineGoods, payload);
      if (!response) return;
      notification.success({
        message: '温馨提示',
        description: '下架成功',
      });
      callback && callback();
    },
    *fakeUpOfflineGoods({ payload, callback }, { call, put }) {
      const response = yield call(fakeUpOfflineGoods, payload);
      if (!response) return;
      notification.success({
        message: '温馨提示',
        description: '上架成功',
      });
      callback && callback();
    },
    *fakeCheckRemoveGoods({ payload, callback }, { call, put }) {
      const response = yield call(fakeCheckRemoveGoods, payload);
      if (!response) return;
      const { removeCheckListGoodsInfoList = [] } = response.content;
      callback && callback(removeCheckListGoodsInfoList);
    },

    *fakeDeleteOfflineGoods({ payload, callback }, { call, put }) {
      const response = yield call(fakeDeleteOfflineGoods, payload);
      if (!response) return;
      notification.success({
        message: '温馨提示',
        description: '删除成功',
      });
      callback && callback();
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
